package com.qs.eggs.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.RatingBar;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.qs.eggs.Constants;
import com.qs.eggs.R;
import com.qs.eggs.bean.CollectBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class SubEvaluationdapter extends SimpleAdapter<CollectBean> implements BaseAdapter.OnItemClickListener {
    //定义一个HashMap，用来存放EditText的值，Key是position
    HashMap<Integer, String> hashMap = new HashMap<Integer, String>();
    HashMap<Integer, String> hashMap1 = new HashMap<Integer, String>();
    private RatingBar ratingBar;
    private TextView mComment;
    private List<CollectBean> datas;
    private ArrayList<HashMap<String,String>> mInfo;
    private String s;
    public SubEvaluationdapter(Context context, List<CollectBean> datas) {
        super(context, R.layout.item_sub_evaluation, datas);

        this.datas=datas;
    }

    @Override
    protected void convert(BaseViewHolder viewHoder, final CollectBean item, final int position) {

        mComment= viewHoder.getTextView(R.id.et_comment);
        TextView proname=(TextView)viewHoder.getView(R.id.proname);
        ratingBar=(RatingBar)viewHoder.getView(R.id.room_ratingbar);
        SimpleDraweeView draweeView = (SimpleDraweeView) viewHoder.getView(R.id.propic);
        draweeView.setImageURI(Constants.URL_BASE+"productimages/"+item.getAutoname());
        proname.setText(item.getProname());
        ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar1, float rating, boolean fromUser) {
                hashMap1.put(position, (int) Math.rint(ratingBar1.getRating()) + "");

            }
        });
        if(hashMap1.get(position) != null){
           ratingBar.setRating(Float.valueOf(hashMap1.get(position)));
        }
        mComment.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start,
                                          int count,int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                //将editText中改变的值设置在HashMap中
                hashMap.put(position, s.toString());
            }
        });

        //如果hashMap不为空，就设置的editText
        if(hashMap.get(position) != null){
           mComment.setText(hashMap.get(position));
        }

    }

    @Override
    public void onItemClick(View view, int position) {

    }

    public ArrayList<HashMap<String,String>> getInfo(){
    if (TextUtils.isEmpty(mComment.getText().toString())) {
        mInfo = null;
    }else if ((int) Math.rint(ratingBar.getRating())==0){
        mInfo=null;
     } else {
        mInfo=new ArrayList<>();
        for (int i=0;i<datas.size();i++) {
            HashMap<String, String> map = new HashMap<>();
                map.put("proid", datas.get(i).getProid());
                map.put("proname", datas.get(i).getProname());
                map.put("type", datas.get(i).getType());
                map.put("comments", hashMap.get(i));
                map.put("scores", hashMap1.get(i));
                mInfo.add(map);
            }
        }
        return mInfo;
    }
}
